package com.yunmang.fragment.qianshou;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.yunmang.R;
import com.yunmang.activity.qianshou.SignDetailActivity;
import com.yunmang.adapter.NosendAdapter;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.ParameterEntity.SignBean;
import com.yunmang.entity.event.SignEvent;
import com.yunmang.fragment.BaseFragment;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/10.
 */
public class NoSendFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    @InjectView(R.id.ll_top_search)
    LinearLayout ll_top;
    @InjectView(R.id.et_search)
    EditText etSearch;
    @InjectView(R.id.iv_scan)
    ImageView ivScan;
    @InjectView(R.id.btn_search)
    Button btnSearch;
    @InjectView(R.id.list)
    ListView list;
    private Subscriber subscriber;
    private List<SignBean.ModelBean.ItemsBean> itemList,dataList;
    private NosendAdapter adapter;
    private SignEvent event;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_nosend;
    }

    @Override
    public void initView(View view) {
        ButterKnife.inject(this, view);
        EventBus.getDefault().register(this);
        ll_top.setVisibility(View.GONE);
    }

    @Override
    public void initData() {
        downSignInfo("");
    }


    /**
     * 下载信息
     *
     * @param hdbh
     */
    private void downSignInfo(String hdbh) {
        showWaitingDialog("数据加载中……");
        subscriber = new Subscriber<SignBean>() {
            @Override
            public void onCompleted() {
                hideWaitingDialog();
            }

            @Override
            public void onError(Throwable e) {
                hideWaitingDialog();
            }

            @Override
            public void onNext(SignBean signBean) {
                hideWaitingDialog();
                if (signBean.getCode() == 200) {
                    itemList = signBean.getModel().getItems();
                    if(itemList.size()==0){
                        CommonUtils.showToast(getActivity(),"暂无数据");
                        return;
                    }
                    adapter = new NosendAdapter(context, itemList);
                    list.setAdapter(adapter);
                    list.setOnItemClickListener(NoSendFragment.this);
                } else {
                    CommonUtils.showToast(getActivity(),"下载失败");
                }
            }
        };
        HttpUtils.getInstance().downloadSignInfo(subscriber, "",hdbh, AppConstant.orgInfo.getId() + "");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    @OnClick(R.id.btn_search)
    public void onClick() {
        CommonUtils.hideSoftInput(getActivity());
        String hdbh= CommonUtils.getTextViewValue(etSearch);
        if(TextUtils.isEmpty(hdbh)){
            CommonUtils.showToast(getActivity(),"请输入运单号");
            return;
        }
        downSignInfo(hdbh);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        SignBean.ModelBean.ItemsBean itemsBean = itemList.get(i);
        event=new SignEvent();
        event.setHdbh(itemsBean.getHdbh());
        event.setShipper(itemsBean.getShipper());
        event.setShipperPhone(itemsBean.getShipperPhone());
        event.setGoodsCost(itemsBean.getGoodsCost());
        event.setGoodsName(itemsBean.getGoodsName());
        event.setId(itemsBean.getId());
        event.setNumbers(itemsBean.getNumbers());
        event.setTakeMethod(itemsBean.getTakeMethod());
        event.setBalanceMethod(itemsBean.getBalanceMethod());
        event.setEndOrgName(itemsBean.getEndOrgName());
        event.setTransportCost(itemsBean.getTransportCost());
        event.setStartDepartName(itemsBean.getStartDepartName());
        event.setStartOrgName(itemsBean.getStartOrgName());
        event.setStatus(itemsBean.getStatus());
        String tag;
        if(itemsBean.getTakeMethod().trim().equals("自提")){
            tag="自提";
        }else{
            tag="送货";
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                EventBus.getDefault().post(event);
            }
        },800);

       Intent in= new Intent(getActivity(), SignDetailActivity.class);
        startActivity(in);
    }

    public void onEvent(String event){
        if(event.equals("refresh")){
            downSignInfo("");
        }else{
            //需要遍历集合
            dataList=new ArrayList<>();
            for(int i=0;i<itemList.size();i++){
                if(event.equals(itemList.get(i).getId()+"")){
                    dataList.add(itemList.get(i));
                    adapter.refresh(dataList);
                    return;
                }
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
